package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import BizLogic.LoginCredentialsManager;

/**
 * Servlet implementation class ChangePassword
 */
@WebServlet("/ChangePassword")
public class ChangePassword extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangePassword() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String NewPassword=request.getParameter("NewPassword");
		String ConfirmPassword=request.getParameter("ConfirmPassword");
		LoginCredentialsManager lcm=new LoginCredentialsManager();
		HttpSession StaffSession=request.getSession();
		String StaffID=StaffSession.getAttribute("StaffID").toString();
		if(NewPassword.trim().equals("")||ConfirmPassword.trim().equals("")){
			request.setAttribute("result", "New password or Confirm password is empty!");
			RequestDispatcher rdDispatcher=request.getRequestDispatcher("ChangePassword.jsp");
			rdDispatcher.forward(request, response);
		}
		else{
		if(lcm.checkPassword(StaffID,NewPassword, ConfirmPassword)){
			request.setAttribute("result", "Change password sucessfully!");
			RequestDispatcher rdDispatcher=request.getRequestDispatcher("Login.jsp");
			rdDispatcher.forward(request, response);
			
		}
		else
		{
			request.setAttribute("result", "New password is not same with Confirm password!");
			RequestDispatcher rdDispatcher=request.getRequestDispatcher("ChangePassword.jsp");
			rdDispatcher.forward(request, response);
			
		}
		}
	}

}
